package com.fbinggun.dt.task.conditions;

import com.fbinggun.dt.task.Task;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.player.PlayerMoveEvent;

import java.sql.SQLException;

public class VehicleMove extends Conditions{

    String type;

    public VehicleMove(String id, ConfigurationSection config, Task task) {
        super(id, config, task);
        this.type=config.getString("Type");
    }

    @EventHandler
    public void move(PlayerMoveEvent event) throws SQLException, ClassNotFoundException {
        Player player=event.getPlayer();
        Entity entity = player.getVehicle();
        if(entity==null){
            return;
        }
        String entityName = entity.getType().name().toUpperCase();
        if(isRun(event.getPlayer())){
            if(type==null){
                addCount(event.getPlayer(), event.getFrom().distance(event.getTo()));
                return;
            }
            if(type.contains(entityName)) {
                addCount(event.getPlayer(), event.getFrom().distance(event.getTo()));
            }
        }
    }
}
